//
//  WSSearchResultController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/1.
//

#import "WSSearchResultController.h"
#import "WSSearchResultCell.h"
#import "WSSearchRequest.h"
#import "WSSearchFootView.h"
#import "CTMediator+WSPlayerAction.h"

@interface WSSearchResultController () <WSSearchFootViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *listView;

@property (nonatomic, strong) WSSearchRequest * request;
@property (strong, nonatomic) NSArray * hotSearchArray;

@end

@implementation WSSearchResultController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.request = [WSSearchRequest new];
    [self setEmptyStateTitle:APPLocalized(@"抱歉～！沒有搜索到相關內容\n可以看看其他推薦哦～") state:ListViewEmptyStateEmpty];
}

- (void)searchcharonTV_WithKeyWord:(NSString *)keyWord {
    self.request.keycharonTV_Word = keyWord;
    [self refreshData];
}

#pragma mark RefreshResultProtocol
- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    [super loadSuccessWithArray:array isRefresh:isRefresh];
    if ([self.delegate respondsToSelector:@selector(searchResultControllerSearchSuccess:)]) {
        [self.delegate searchResultControllerSearchSuccess:self.request.keycharonTV_Word];
    }
    [self.listView reloadData];
}

- (void)loadFailedWithError:(NSError *)error {
    [super loadFailedWithError:error];
}

- (BOOL)disableAutoBeginRefresh {
    return NO;
}

#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSSearchResultCell * searchResultCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSearchResultCell" forIndexPath:indexPath];
    [searchResultCell setDataWithObject:self.dataArray[indexPath.row]];
    return searchResultCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth - 12 - 12;
    size.height = 105 * 4 / 3.0f;
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        WSSearchFootView * charonTV_searchFootView;
        if (!charonTV_searchFootView) {
            charonTV_searchFootView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"WSSearchFootView" forIndexPath:indexPath];
            charonTV_searchFootView.delegate = self;
            [charonTV_searchFootView setDataWithObject:self.hotSearchArray];
        }
        return charonTV_searchFootView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth;
    if (self.hotSearchArray.count > 0 && self.dataArray.count == 0) {
        size.height = (36 + 10) * 5;
    }
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WSVideo * video = self.dataArray[indexPath.row];
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PlayerWithVideo:video];
    [self.navigationController pushViewController:target animated:YES];
}

#pragma mark WSSearchFootViewDelegate
- (void)searchFootViewWithObjct:(WSVideo *)object {
    if ([self.delegate respondsToSelector:@selector(searchResultWithKeyWord:)]) {
        [self.delegate searchResultWithKeyWord:object.title];
    }
}

- (NSArray *)hotSearchArray {
    if ([self.delegate respondsToSelector:@selector(searchResultControllerHotSearchArray)]) {
        return [self.delegate searchResultControllerHotSearchArray];
    }
    else {
        return @[];
    }
}
@end
